<script setup>
import { onMounted, ref } from 'vue'
import { queryPageApi } from '@/api/role'

// 查询参数
const queryParams = ref({
  name: '',
  label: '',
  page: 1,
  pageSize: 10
})

// 表格数据
const roleList = ref([])
const total = ref(0)

// 搜索
const search = async() => {
  const result = await queryPageApi(queryParams.value)
  if(result.code === 1){
    roleList.value = result.data.rows;
    total.value = result.data.total;
  }
}

// 清空
const clear = () => {
  queryParams.value = {name:'',label:'',page:1,pageSize:10}
  search();
}

// 添加角色
const handleAdd = () => {
  console.log('添加角色')
}

// 修改角色
const handleEdit = (id) => {
  console.log('修改角色:', id)
}

// 删除角色
const handleDelete = (id) => {
  console.log('删除角色:', id)
}

// 分页大小改变
const handleSizeChange = (val) => {
  search();
}

// 页码改变
const handleCurrentChange = (val) => {
  search();
}

//页面加载时刷新
onMounted(()=>{
  search();
})

</script>

<template>
  <div class="role-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" class="search-form">
      <el-form-item label="角色名称">
        <el-input 
          v-model="queryParams.name" 
          placeholder="请输入角色名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="角色标识">
        <el-input 
          v-model="queryParams.label" 
          placeholder="请输入角色标识"
          clearable
        />
      </el-form-item>
      <el-form-item class="search-buttons">
        <el-button type="primary" @click="search">
          <el-icon><Search /></el-icon>搜索
        </el-button>
        <el-button @click="clear">
          <el-icon><Refresh /></el-icon>清空
        </el-button>
      </el-form-item>
    </el-form>
    
    <!-- 操作按钮区域 -->
    <div class="operation-area">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>添加角色
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table :data="roleList" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="name" label="角色名称" width="200" align="center" />
      <el-table-column prop="label" label="角色标识" width="200" align="center" />
      <el-table-column prop="updateTime" label="最后修改时间" width="200" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="handleEdit(scope.row.id)">
            <el-icon><Edit /></el-icon>修改
          </el-button>
          <el-button type="danger" link @click="handleDelete(scope.row.id)">
            <el-icon><Delete /></el-icon>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[5, 10, 20, 40, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.role-container {
  padding: 20px;
  background-color: #fff;
  min-height: 100%;
  border-radius: 8px;
}

.search-form {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.search-form :deep(.el-form-item) {
  margin-right: 20px;
  min-width: 280px;
}

.search-buttons {
  margin-left: auto !important;
  min-width: auto !important;
}

.operation-area {
  margin: 20px 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-button .el-icon) {
  margin-right: 4px;
}
</style>
